const mongoose = require('mongoose');

const aiAssistantSchema = new mongoose.Schema(
  {
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      required: true,
    },
    name: {
      type: String,
      default: '豆包',
      trim: true,
    },
    avatar: {
      type: String,
      default: 'default-assistant-avatar.png',
    },
    personality: {
      type: String,
      enum: ['友好', '专业', '幽默', '鼓励', '挑战'],
      default: '友好',
    },
    level: {
      type: Number,
      default: 1,
      min: 1,
      max: 10,
    },
    specialties: [{
      type: String,
      enum: ['逻辑思维', '创意设计', '算法', '游戏开发', '动画制作', '音乐创作', '故事叙述'],
    }],
    interactions: [
      {
        timestamp: {
          type: Date,
          default: Date.now,
        },
        userQuery: String,
        assistantResponse: String,
        context: {
          projectId: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Project',
          },
          codeSnippet: String,
          learningGoal: String,
        },
        feedback: {
          helpful: Boolean,
          rating: {
            type: Number,
            min: 1,
            max: 5,
          },
          comment: String,
        },
      },
    ],
    learningProfile: {
      strengths: [String],
      areasToImprove: [String],
      recommendedConcepts: [String],
      learningPath: {
        currentStage: String,
        nextConcepts: [String],
        completedConcepts: [String],
      },
    },
    preferences: {
      explanationStyle: {
        type: String,
        enum: ['详细', '简洁', '类比', '可视化', '互动式'],
        default: '详细',
      },
      challengeLevel: {
        type: String,
        enum: ['简单', '适中', '挑战'],
        default: '适中',
      },
      interactionFrequency: {
        type: String,
        enum: ['低', '中', '高'],
        default: '中',
      },
    },
    usageStats: {
      totalInteractions: {
        type: Number,
        default: 0,
      },
      helpfulResponses: {
        type: Number,
        default: 0,
      },
      averageRating: {
        type: Number,
        default: 0,
      },
      lastInteractionDate: Date,
      mostFrequentTopics: [String],
    },
  },
  {
    timestamps: true,
  }
);

// 创建索引以提高查询性能
aiAssistantSchema.index({ user: 1 });
aiAssistantSchema.index({ 'interactions.timestamp': -1 });

const AIAssistant = mongoose.model('AIAssistant', aiAssistantSchema);

module.exports = AIAssistant;